﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_Video_ctrCountryVideo : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    private int nItemID = 0;
    private int nPageSize = 6;

    private int nCountryID = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            BigVideo(nCountryID);
        }
    }
    public void setCountry(int CountryID)
    {
        nCountryID = CountryID;
    }

    private void BigVideo(int nCountryID)
    {
        Video objVideo = new Video();
        DataTable dtb = new DataTable();
        int nPageCount = 0;
        int DefaultLanguage = Support.GetDefaultLanguage();
        int nCurrentLang = Support.GetCurrentLanguage();
        int nVideoID = ConvertData.ConvertToInt(Request.QueryString["vid"]);
        if (nVideoID > 0)
        {
            Video objGetCountry = new Video();
            objGetCountry.LoadById(nVideoID);
            if (objGetCountry.Data.CountryID == nCountryID)
            {
                objVideo.LoadById(nVideoID);
                string sVideoCode = ConvertData.ConvertToString(objVideo.Data.URL);
                sVideoCode = sVideoCode.Replace("250", "662").Replace("215", "450");
                ltrBigVideo.Text = sVideoCode;
                dtb = objVideo.Search(0, nVideoID, "", nCountryID, DefaultLanguage, nCurrentLang, 1, 1, nPageSize, ref nPageCount);
                BindData(1, nVideoID, nCountryID);
            }
            else//Same
            {
                dtb = objVideo.Search(0, 0, "", nCountryID, DefaultLanguage, nCurrentLang, 1, 1, nPageSize, ref nPageCount);
                int nCount = dtb.Rows.Count;

                if (nCount > 0)
                {
                    string sCode = ConvertData.ConvertToString(dtb.Rows[0]["URL"]);
                    nItemID = ConvertData.ConvertToInt(dtb.Rows[0]["VideoID"]);
                    sCode = sCode.Replace("250", "662").Replace("215", "450");

                    ltrBigVideo.Text = sCode;
                    BindData(1, nItemID, nCountryID);
                }
            }
        }
        else//Same
        {
            dtb = objVideo.Search(0, 0, "", nCountryID, DefaultLanguage, nCurrentLang, 1, 1, nPageSize, ref nPageCount);
            int nCount = dtb.Rows.Count;

            if (nCount > 0)
            {
                string sCode = ConvertData.ConvertToString(dtb.Rows[0]["URL"]);
                nItemID = ConvertData.ConvertToInt(dtb.Rows[0]["VideoID"]);
                sCode = sCode.Replace("250", "662").Replace("215", "450");

                ltrBigVideo.Text = sCode;
                BindData(1, nItemID, nCountryID);
            }
        }
    }
   
    private void BindData(int CurrentPageIndex, int nItem, int nCountryID)
    {
        try
        {
            DataTable dtb = new DataTable();
            Video objVideo = new Video();
            int nPageCount = 0;
            int DefaultLanguage = Support.GetDefaultLanguage();
            int nCurrentLang = Support.GetCurrentLanguage();
            dtb = objVideo.Search(0, nItem, "", nCountryID, DefaultLanguage, nCurrentLang, 1, CurrentPageIndex, nPageSize, ref nPageCount);
            rptVideo.DataSource = dtb;
            rptVideo.DataBind();

            VisiblePaging(dtb.Rows.Count);
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    protected void rptVideo_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

    }
    protected void rptVideo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {

            Literal ltrVideo = (Literal)e.Item.FindControl("ltrVideo");
            HyperLink lnkVideo = (HyperLink)e.Item.FindControl("lnkVideo");
            Label lblInitContent = (Label)e.Item.FindControl("lblInitContent");

            lnkVideo.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "VideoTitle"));
            lblInitContent.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
            string sCode = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "URL"));
            sCode = sCode.Replace("250", "150").Replace("215", "120");
            ltrVideo.Text = sCode;

            lnkVideo.NavigateUrl = "~/Videos.aspx?vid=" + ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "VideoID"));
        }
    }

    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }
    private void initDataPaging()
    {
        Pager.PageSize = nPageSize;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx, nItemID, nCountryID);
    }
    private void VisiblePaging(int intPagecount)
    {
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
}